﻿using BaseAuthRole.Domain.Input;
using BaseAuthRole.Domain.OutPut;
using BaseAuthRole.DTO;
using BaseAuthRole.IRepository;
using BaseAuthRole.IService;
using BaseAuthRole.Web.Filters;
using Lotus.AspNetCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RS.BaseModel.Common;
using RS.Environment;
using RS.Tool.Base;
using System.Web;

namespace BaseAuthRole.Web.Api
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GateWayController : ControllerBase
    {
        private IUserService _userService;

        private IApplicationService _applictationService;

        private IFreeSql _authRepository;

        private IBaseLotusClient _baseLotusClient;
        public GateWayController(IUserService userService, IApplicationService applictationService, IFreeSql authRepository, IBaseLotusClient baseLotusClient)
        {   
            this._userService = userService;
            this._applictationService = applictationService;
            this._authRepository = authRepository;
            this._baseLotusClient = baseLotusClient;
        }

        [HttpGet]
        public string Get()
        {
            var authStr = JsonConvert.SerializeObject(new
            {
                appId = "802639885746831360",
                appSecret = "OGNgqZMr"
            });
            return SecretHelper.RSAEncrypt(authStr, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuD/eQtZ2SUpGUz4MGFUBWXn+2IAUsutdQ6Se4/bQpxZJCjMsbYxU22086V+4fEfl1SV3z3AW4a23OTuaMxkJlH6sISqQD/2YfT+RJZAa7CupaB2IWWhTNesWe4tk+Q2BFxCufGPM26aXyRggf5J4HUOlqGSEosDkiEstAAlOH4FfIYJaTQgZ7x3gntVydk8XRM7mjkRuhU64NS75QpmarK+mWgTrFdOul08asPueKq6vLyyXuPP1UMQkL36hGPu0Gof8u7kzBQlJsu0aZmGv/IOr4uofadZN58bTWTdo3aNS7c5pp6QF4yhJaxOqmmgHvWHY5zc5wGdTqCyO8yHeQQIDAQAB", "", false);
        }

        [HttpGet]
        public IActionResult CheckApp(string appid, string secret) {

            var result = new CommonResult();

            if (string.IsNullOrEmpty(appid) || string.IsNullOrEmpty(secret))
            {
                result.Message = "必要参数为空";
            }
            else
            {

                var r = _applictationService.CheckApp(appid, secret);
                result = r;
            }

            return Ok(result);

        }
       
        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetBaseAuthConfig(string appid, string appAccessToken) { 
        
            
            var result=new CommonResult<GetBaseAuthConfigOutPut>();


            result.IsError = false;

            result.Result = new GetBaseAuthConfigOutPut
            {

                RedisConfig = ConfigurationHelper.GetAppSettings("RedisConfig")

            };
            
            return Ok(result); 
        
        }


        [HttpGet]
        public IActionResult GetAppAccessToken(string appid, string secret)
        {
            var result = new CommonResult<GetAppAccessTokenModelOutPut>();

            if (string.IsNullOrEmpty(appid) || string.IsNullOrEmpty(secret))
            {
                result.Message = "必要参数为空";
            }
            else
            {

                var r = _userService.GetAppAccessTokenModel(appid,secret);
                result = r;
            }

            return Ok(result);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult CheckAppAccessToken(string appid,string appaccesstoken) {

            var result = this._userService.AccessTokenCheck(appid, appaccesstoken);           

            return Ok(result);
        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserAuthToken([FromQuery] GetUserAuthTokenInput getUserAuthTokenInput)
        {

            var r = this._userService.GetUserAuthToken(getUserAuthTokenInput.AppId, getUserAuthTokenInput.AuthCode);

            return Ok(r);
        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserInfo([FromQuery] GetUserInfoInput getUserInfoInput)
        {
            var r = this._userService.GetUserInfo(getUserInfoInput);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserDetailInfo([FromQuery] GetUserInfoInput getUserInfoInput)
        {
            var r = this._userService.GetUserDetailInfo(getUserInfoInput);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserInfoList([FromQuery] GetUserInfoListInput input)
        {
            var r = this._userService.GetUserInfoList(input);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetSimpleUserInfoList([FromQuery] GetUserInfoListInput input)
        {
            var r = this._userService.GetSimpleUserInfoList(input);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserInfoByUid([FromQuery] GetUserInfoByUidInput input) {

            var r = this._userService.GetUserInfoByUid(input);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserInfoByJobNumber([FromQuery] GetUserInfoByJobNumberInput input)
        {

            var r = this._userService.GetUserInfoByJobNumber(input);
            return Ok(r);

        }

        [AppAccessCheckFilter]
        [HttpGet]
        public IActionResult GetUserInfoByLoginName([FromQuery] GetUserInfoByLoginNameInput input)
        {

            var r = this._userService.GetUserInfoByLoginName(input);
            return Ok(r);

        }

        [HttpGet]
        public IActionResult GetOrgIdListByOrgId([FromQuery] GetOrgIdListByOrgIdInput input)
        {
            var r = this._userService.GetOrgIdListByOrgId(input);
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetSubOrgIdListByOrgId([FromQuery] GetOrgIdListByOrgIdInput input)
        {
            var r = this._userService.GetSubOrgIdListByOrgId(input);
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetAllSimpleOrgList()
        {
            var r = this._userService.GetAllSimpleOrgList();
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetOrgInfoByOrgId([FromQuery] GetOrgInfoByOrgIdInput input)
        {
            var r = this._userService.GetOrgInfoByOrgId(input);
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetOrgInfosByOrgIds([FromQuery] GetOrgInfosByOrgIdsInput input)
        {
            var r = this._userService.GetOrgInfosByOrgIds(input);
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetKnowledgeAuthInfoList([FromQuery] GetKnowledgeAuthInfoListInput input)
        {
            var r = this._userService.GetKnowledgeAuthInfoList(input);
            return Ok(r);
        }

        [HttpGet]
        public IActionResult GetKnowledgeAuthOrganizationList([FromQuery] GetKnowledgeAuthOrganizationApiListInput apiInput)
        {
            return Ok(_userService.GetKnowledgeAuthOrganizationApiList(apiInput));
        }

        [HttpGet]
        public IActionResult UpdateKnowledgeReadAction([FromQuery] UpdateKnowledgeReadActionInput input)
        {
            input.ActionName = HttpUtility.UrlDecode(input.ActionName);
            input.OriActionName =  HttpUtility.UrlDecode(input.OriActionName);
            input.ControlName =  HttpUtility.UrlDecode(input.ControlName);
            input.Description = HttpUtility.UrlDecode(input.Description);
            return Ok(this._userService.UpdateKnowledgeReadAction(input));
        }
    }
}
